package com.commerce.addressbook.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.commerce.addressbook.Entity.Contacts;
import com.commerce.addressbook.Entity.DataBean;
import com.commerce.addressbook.GlobalVariable;
import com.commerce.addressbook.R;
import com.commerce.addressbook.adapter.MyListViewAdapter;
import com.commerce.addressbook.utlis.MyContentResolver;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class ContactsActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView lxr;
    private EditText mz;
    private ListView list;
    private Button bc;
    private Button sclxr;
    private List<DataBean> listdata = new ArrayList<>();
    private MyListViewAdapter myListViewAdapter;
    private Bundle bundle;
    private Contacts contacts;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contacts);
        initView();
    }

    private void initView() {
        lxr = findViewById(R.id.lxr);
        mz = findViewById(R.id.mz);
        list = findViewById(R.id.list);
        bc = findViewById(R.id.bc);
        sclxr = findViewById(R.id.sclxr);
        Intent intent = getIntent();
        int pos = intent.getIntExtra("pos", 0);
        bundle = intent.getExtras();
        if(bundle !=null){
            contacts = (Contacts) bundle.getSerializable("mParents");
            if(contacts.getImgUrl()!=null) {
                Glide.with(this).load(contacts.getImgUrl())
                        .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                        .into(lxr);
            }
            mz.setText(contacts.getContacts());
            List<List<DataBean>> dataBeans = GlobalVariable.getGlobalVariable().getDataBeans();
            listdata = dataBeans.get(pos);
            listdata.remove(listdata.size()-1);
            myListViewAdapter = new MyListViewAdapter(listdata);
        }else {
            myListViewAdapter = new MyListViewAdapter(new ArrayList<DataBean>());
        }
        list.setAdapter(myListViewAdapter);
        bc.setOnClickListener(this);
        sclxr.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.contacts,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        finish();
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        List<DataBean> list = myListViewAdapter.getList();
        String name = mz.getText().toString();
        switch (v.getId()){
            case R.id.bc :
                if(list!=null&&list.size()>0){
                    if(!list.get(0).getIds().equals("-1")){
                        new MyContentResolver(this).delContacts(contacts.getIds(),contacts.getId(),false);
                    }
                    //新增联系人
                    if(name.equals("")){
                        new MyContentResolver(this).ContactsInsert(list.get(0).getPhone(),list);
                    }else {
                        new MyContentResolver(this).ContactsInsert(name,list);
                    }
                }
                break;
            case R.id.sclxr:
                if(bundle!=null){
                    new MyContentResolver(this).delContacts(contacts.getIds(),contacts.getId(),true);
                }
                break;
        }
        finish();
    }
}